package com.wsy.controller;

import com.github.pagehelper.PageInfo;
import com.wsy.entity.Emp;
import com.wsy.entity.ResponseMsg;
import com.wsy.entity.Role;
import com.wsy.service.EmpService;
import com.wsy.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("emp")
@Slf4j
public class EmpController {
    @Resource
    EmpService empService;
    @Resource
    RoleService roleService;

    @RequestMapping("test")
    public String select() {
        log.info("调用>>>>>>>>>>>>>");
        return "ok";
    }


    @RequestMapping("getAuthUser")
    public Authentication getAuthUser(HttpServletResponse httpServletResponse,Authentication authentication){
        // 响应设置
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setHeader("Content-Type", "application/json");
        httpServletResponse.setStatus(200);
        String name = authentication.getName();
        log.info("name>>>>"+name);

        Object principal = authentication.getPrincipal();
        log.info(principal.toString());

        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        log.info(authorities.toString());

        return authentication;
    }

    @RequestMapping("select")
    public PageInfo<Emp> select(Integer pageNum, Integer pageSize) {
        return empService.select(pageNum, pageSize);
    }

    @RequestMapping("insert")
    public ResponseMsg addEmp(Emp emp) {
        return new ResponseMsg(200,"添加成功",empService.addEmp(emp));
    }

    @RequestMapping("selRole")
    public List<Role> selRole() {
        return roleService.list();
    }

    @RequestMapping("update")
    public ResponseMsg updEmp(Emp emp) {
        return new ResponseMsg(200,"修改成功",empService.updEmp(emp));
    }

    @RequestMapping("delete")
    public ResponseMsg delEmp(Emp emp) {
        return new ResponseMsg(200,"删除成功",empService.delEmp(emp));
    }
}
